package com.fhyc.controller;

import com.fhyc.common.XPage;
import com.fhyc.dto.AccuracyIndexDTO;
import com.fhyc.dto.LoadCurveShowDto;
import com.fhyc.service.OperationalMonitorService;
import com.fhyc.vo.ResponseResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;

/**
 * 母线负荷预测
 */
@Slf4j
@RestController
@RequestMapping("/api/fm/operationalMonitor")
public class OperationalMonitorController {

    @Resource
    private OperationalMonitorService operationalMonitorService;

    /**
     * 指定日期的母线预测值和实际值查询
     *
     * @param dataDate 查询日期yyyy-MM-dd
     * @param busId    母线id
     * @param teamId   算法id
     * @return
     */
    @GetMapping("/queryPredictedAndActualBusValues")
    public ResponseResultVO<LoadCurveShowDto> queryPredictedAndActualBusValues(@RequestParam String dataDate,
                                                                               @RequestParam String busId,
                                                                               @RequestParam(defaultValue = "1") Integer teamId) throws IOException, ParseException {
        LocalDate localDate = LocalDate.parse(dataDate); // 转换为 LocalDate
        LoadCurveShowDto resp = operationalMonitorService.queryPredictedAndActualBusValues(localDate, busId, teamId);
        return new ResponseResultVO<>(resp);
    }

    /**
     * 准确率指标查询
     *
     * @param pageNum  页数
     * @param pageSize 页面大小
     * @param dataDate 查询日期yyyy-MM-dd
     * @param busId    母线id  example = "4604"
     * @return
     */
    @GetMapping("/queryAccuracyIndex")
    public ResponseResultVO<XPage<AccuracyIndexDTO>> queryAccuracyIndex(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                                                        @RequestParam(required = false) String dataDate,
                                                                        @RequestParam(required = false) String busId) throws NoSuchFieldException, IllegalAccessException {


        LocalDate localDate = LocalDate.parse(dataDate); // 转换为 LocalDate

        //XPage<AccuracyIndexDTO> page = new XPage<>(pageNum, pageSize);
        XPage<AccuracyIndexDTO> resp = operationalMonitorService.queryAccuracyIndex(pageNum, pageSize, localDate, busId);
        return new ResponseResultVO<>(resp);
    }

    /**
     * 准确率指标数据表格导出
     *
     * @param response
     * @param dataDate
     * @param busId
     */
    @GetMapping("/AccuracyIndexExportExcel")
    public void AccuracyIndexExportExcel(HttpServletResponse response,
                                         @RequestParam(required = false) String dataDate,
                                         @RequestParam(required = false) String busId) {
        try {
            LocalDate localDate = LocalDate.parse(dataDate); // 转换为 LocalDate
            operationalMonitorService.AccuracyIndexExportExcel(response, localDate, busId);
        } catch (Exception e) {
            log.error(e.toString());
            throw new RuntimeException(e);
        }
    }

}
